/*
 * @Author: xiaoyang
 * @Date: 2021-04-26 17:55:12
 * @LastEditors: xiaoyang
 * @LastEditTime: 2021-04-29 14:08:08
 * @FilePath: \ruoyi-uif:\program_code\move_bzypt\main\js\loan-choose.js
 * @description: No description
 */

new Vue({
  el: '#loan-choose',
  data: {
    msg: 'helloworld',
    percent: 3.25,
    multiple: 1,
    yetChooseLoan: null,
    // 商业利率选择
    businessList: [
      { name: '最新商业贷款基准利率(4.9%)', num: 4.9 },
      { name: '最新商业贷款基准利率上浮40%(6.86%)', num: 6.86 },
      { name: '最新商业贷款基准利率上浮20%(5.88%)', num: 5.88 },
      { name: '最新商业贷款基准利率上浮15%(5.64%)', num: 5.64 },
      { name: '最新商业贷款基准利率上浮10%(5.39%)', num: 5.39 },
      { name: '最新商业贷款基准利率上浮5%(5.15%)', num: 5.15 },
      { name: '最新商业贷款基准利率下浮5%(4.66%)', num: 4.66 },
      { name: '最新商业贷款基准利率下浮10%(4.41%)', num: 4.41 },
      { name: '最新商业贷款基准利率下浮15%(4.17%)', num: 4.17 },
      { name: '最新商业贷款基准利率下浮20%(3.92%)', num: 3.92 },
      { name: '最新商业贷款基准利率下浮30%(3.43%)', num: 3.43 },
    ],
    // 公积金利率选择
    accumulationList: [
      { name: '最新公积金基准利率(3.25%)', num: 3.25 },
      { name: '最新公积金基准利率(3.9%)', num: 3.9 },
      { name: '最新公积金基准利率(3.58%)', num: 3.58 },
    ],

    // 数据赋值
    list: [],
  },
  methods: {
    /**
     * @description: 确定按钮事件
     * @param {*}
     * @return {*}
     */
    handleRateChoose() {
      const { percent, multiple } = this;

      let type = getQueryVariable('type');

      if (!percent) {
        this.dialog({ title: '输入提示', message: '请输入正确的自定义利率' });
        return;
      }
      if (!multiple) {
        this.dialog({ title: '输入提示', message: '请输入正确的自定义倍数' });
        return;
      }

      let data = JSON.parse(Cookies.get('data') || null);

      if (type == 1) {
        data.oldLoanLPR = percent * multiple;
      } else if (type == 2) {
        data.accumulationFundLoanTitle = percent * multiple;
      }

      Cookies.set('data', JSON.stringify(data), { expires: 1 });
      window.location.replace('../../loan.html');
    },

    /**
     * @description:输入框输入时对数字进行截取 因为设置maxlength属性无效
     * @param {*}
     * @return {*}
     */
    handleInput(e) {
      let attr = e.currentTarget.dataset.attr;
      let val = e.currentTarget.value;
      val = val.length > 5 ? val.substr(0, 5) : val;
      val = val[0] === '.' ? '0' + val : val;
      this[attr] = val;
    },

    /**
     * @description: 自定义弹窗内容
     * @param {*}
     * @return {*}
     */
    dialog({ title, message }) {
      const { Dialog } = vant;
      Dialog.alert({
        title: title,
        message: message,
        confirmButtonColor: '#409EFF',
        className: 'loan-dialog',
      }).then(() => {
        console.log('on close');
      });
    },

    /**
     * @description:选择贷款利率
     * @param {*}
     * @return {*}
     */
    chooseAccumulationFundLoan(e) {
      if (e.target.nodeName.toLowerCase() === 'li') {
        let loan = e.target.dataset.loan;
        let type = getQueryVariable('type');
        let data = JSON.parse(Cookies.get('data') || null);

        if (type == 1) {
          data.oldLoanLPR = loan;
          Cookies.set('data', JSON.stringify(data), { expires: 1 });
        } else {
          data.accumulationFundLoanTitle = loan;
          Cookies.set('data', JSON.stringify(data), { expires: 1 });
        }

        window.location.replace('../../loan.html');
      }
    },
  },
  mounted() {
    // type=1 商业旧版贷款利率
    // type=2 公积金贷款利率
    let type = getQueryVariable('type');
    let data = JSON.parse(Cookies.get('data') || null);

    if (type == 1) {
      this.list = this.businessList;
      if (data.oldLoanLPR) {
        this.yetChooseLoan = data.oldLoanLPR;
        this.percent = data.oldLoanLPR;
        this.multiple = 1;
      }
    }

    if (type == 2) {
      this.list = this.accumulationList;
      if (data.accumulationFundLoanTitle) {
        this.yetChooseLoan = data.accumulationFundLoanTitle;
        this.percent = data.accumulationFundLoanTitle;
        this.multiple = 1;
      }
    }
  },
});
